package com.leedsoft.news2.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.leedsoft.news2.activity.WebPageActivity;
import com.leedsoft.news2.adapter.NewsAdapter;
import com.leedsoft.news2.R;
import com.leedsoft.news2.model.News;
import com.leedsoft.news2.model.ShowapiResponse;

import java.util.List;

/**
 * Created by Administrator on 2016/12/18.
 */
public class NewsListFragment extends ListFragment {
    /**
     * 消息队列
     */
    private RequestQueue queue;
    /**
     * Adapter适配器
     */
    private NewsAdapter adapter;

    /**
     * 新闻列表集合
     */
    private List<News> newsList;

    private static final String CHANNEL_ID = "CHANNEL_ID";

    private String channelId;

    public static NewsListFragment newInstance(String channelId) {
        Bundle args = new Bundle();
        args.putString(CHANNEL_ID, channelId);
        NewsListFragment fragment = new NewsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        channelId = getArguments().getString(CHANNEL_ID);
        //加载详情新闻页面
        loadHotNewsList();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        News news = newsList.get(position);
        String url = news.getLink();

        //start web view activity....send this url to web view activity
        Intent intent = new Intent();
//        intent.setAction(Intent.ACTION_VIEW);//隐式intent 调用系统浏览器
        intent.setClass(getActivity(), WebPageActivity.class);
        Uri pageUrl = Uri.parse(url);
        intent.setData(pageUrl);
        startActivity(intent);
    }


    private void loadHotNewsList() {
        // 1.创建RequestQueue消息队列
        queue = Volley.newRequestQueue(getActivity());
        //2.创建RequestQueue请求对象
        String url = "https://route.showapi.com/109-35?channelId=" + channelId +
                "&maxResult=20&needAllList=0&needContent=0&needHtml=0&page=1" +
                "&showapi_appid=28581&showapi_sign=afd21e489f474e34b92024bc0faf247c";

        StringRequest req = new StringRequest(url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                //当请求发送成功后的回调  主线程中运行
                //解析JSON  获取List<Music>
                System.out.println(">>>>>>>>" + response);
                Gson gson = new Gson();
                ShowapiResponse res = gson.fromJson(response, ShowapiResponse.class);
                setAdapter(res);

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // 当请求发送失败的回调

            }
        });
        //3.把请求对象添加到请求队列中
        queue.add(req);
    }

    private void setAdapter(ShowapiResponse res) {

        newsList = res.getShowapi_res_body().getPagebean().getContentlist();
        adapter = new NewsAdapter(getActivity(), newsList, queue);
        //给ListView设置headerView // TODO: 2016/12/19 设置每个listView的header view
//        final ImageView imageView = new ImageView(getActivity());
//        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 260);
//        imageView.setLayoutParams(params);
//        imageView.setImageResource(R.mipmap.ic_launcher);
//        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//        getListView().addHeaderView(imageView);
        setListAdapter(adapter);
        //加载热歌榜榜单图片，并且imageView赋值
        //ImageRequest req = new ImageRequest(test, listener, maxWidth, maxHeight, decodeConfig, errorListener)
    }
}
